package com.codesynapse.controller;

import com.codesynapse.dto.AIRequest;
import com.codesynapse.dto.AIResponse;
import com.codesynapse.service.AIModelService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class AIController {

    private final AIModelService aiModelService;

    @PostMapping("/complete")
    @Cacheable(value = "completions", key = "#request.prompt + #request.context")
    public AIResponse completeCode(@RequestBody AIRequest request) {
        long startTime = System.currentTimeMillis();
        String result = aiModelService.generateCompletionWithContext(request);
        
        return AIResponse.builder()
                .code(result)
                .timestamp(LocalDateTime.now())
                .processingTime(System.currentTimeMillis() - startTime)
                .modelName("deepseek-r1:14b")
                .build();
    }

    @PostMapping("/sql")
    @Cacheable(value = "sqlQueries", key = "#request.prompt + #request.dialect")
    public AIResponse generateSQL(@RequestBody AIRequest request) {
        long startTime = System.currentTimeMillis();
        String result = aiModelService.generateSQLWithDialect(request);
        
        return AIResponse.builder()
                .code(result)
                .timestamp(LocalDateTime.now())
                .processingTime(System.currentTimeMillis() - startTime)
                .modelName("deepseek-r1:14b")
                .build();
    }

    @PostMapping("/search")
    @Cacheable(value = "codeSearches", key = "#request.prompt + #request.language + #request.framework")
    public AIResponse searchCode(@RequestBody AIRequest request) {
        long startTime = System.currentTimeMillis();
        String result = aiModelService.searchCodeWithFilters(request);
        
        return AIResponse.builder()
                .code(result)
                .timestamp(LocalDateTime.now())
                .processingTime(System.currentTimeMillis() - startTime)
                .modelName("deepseek-r1:14b")
                .build();
    }

    @PostMapping("/analyze")
    public AIResponse analyzeCode(@RequestBody String code) {
        long startTime = System.currentTimeMillis();
        String result = aiModelService.analyzeCode(code);
        
        return AIResponse.builder()
                .code(code)
                .explanation(result)
                .timestamp(LocalDateTime.now())
                .processingTime(System.currentTimeMillis() - startTime)
                .modelName("deepseek-r1:14b")
                .build();
    }

    @PostMapping("/optimize")
    public AIResponse optimizeCode(@RequestBody String code) {
        long startTime = System.currentTimeMillis();
        String result = aiModelService.optimizeCode(code);
        
        return AIResponse.builder()
                .code(result)
                .timestamp(LocalDateTime.now())
                .processingTime(System.currentTimeMillis() - startTime)
                .modelName("deepseek-r1:14b")
                .build();
    }

    @PostMapping("/test")
    public AIResponse generateTests(@RequestBody String code) {
        long startTime = System.currentTimeMillis();
        String result = aiModelService.generateTestCases(code);
        
        return AIResponse.builder()
                .code(result)
                .timestamp(LocalDateTime.now())
                .processingTime(System.currentTimeMillis() - startTime)
                .modelName("deepseek-r1:14b")
                .build();
    }
} 